package com.xie.test;

import javax.xml.bind.ValidationException;
import java.time.LocalDate;
import java.util.Objects;

/**
 * @author xiezhendong
 * @date 2021/11/2
 */
public class test2 {

    public static void main(String[] args) throws ValidationException {

        test2 test2 = new test2();
        System.out.println(test2.processSwKeijoki(LocalDate.of(2021, 4, 1)));
        //System.out.println(test2.processSwKeijoki(LocalDate.of(2021, 3, 11)));
        System.out.println(test2.processSwKeijoki(LocalDate.of(2022, 10, 1)));
        System.out.println(test2.processSwKeijoki(LocalDate.of(2021, 11, 3)));

    }

    public String processSwKeijoki(LocalDate swKeijoYmd) throws ValidationException {
        if (Objects.isNull(swKeijoYmd)) {
            return null;
        }

        LocalDate startLocalDate = LocalDate.of(2021, 4, 1);
        LocalDate endLocalDate = LocalDate.of(2022, 4, 1);
        int baseNum = 162;

        return processSwKeijoki(swKeijoYmd, startLocalDate, endLocalDate, baseNum);
    }

    private String processSwKeijoki(LocalDate swKeijoYmd, LocalDate startLocalDate, LocalDate endLocalDate, int baseNum) throws ValidationException {
        if (Objects.isNull(swKeijoYmd)) {
            return null;
        }

        if (swKeijoYmd.isBefore(startLocalDate)) {
            throw new ValidationException(String.format("仕訳計上年月日:%s", swKeijoYmd.toString()));
        }

        if ((swKeijoYmd.isAfter(startLocalDate) || swKeijoYmd.isEqual(startLocalDate)) && swKeijoYmd.isBefore(endLocalDate)) {
            return String.valueOf(baseNum);
        }

        startLocalDate = startLocalDate.plusYears(1);
        endLocalDate = endLocalDate.plusYears(1);
        baseNum++;
        return processSwKeijoki(swKeijoYmd, startLocalDate, endLocalDate, baseNum);
    }
}
